import type { Ytime } from '@/type'
import { createGlobalState } from '@vueuse/core'
import { ref, watch } from 'vue'
import { ytimes } from '@/datasource'

export const ytimeStore = createGlobalState(() => {
  const ytimesS = ref<Ytime[]>([])
  const ytimes905S = ref<Ytime[]>([])
  const ytimes906S = ref<Ytime[]>([])
  const ytimes907S = ref<Ytime[]>([])
  const ytimes908S = ref<Ytime[]>([])
  // const idS = ref(4)
  // 下面是模拟登录完成后获取课程预约安排时间表存入缓存
  // 初始化时设置值
  ytimesS.value = ytimes
  updateFilteredLists() // 调用函数来初始化过滤后的列表

  // 监听 ytimesS 的变化并更新过滤后的列表
  watch(
    ytimesS,
    () => {
      updateFilteredLists()
    },
    { deep: true }
  ) // immediate: false 意味着不在初始化时触发

  // 函数来更新过滤后的列表
  function updateFilteredLists() {
    ytimes905S.value = ytimesS.value.filter((item) => item.room === '905')
    ytimes906S.value = ytimesS.value.filter((item) => item.room === '906')
    ytimes907S.value = ytimesS.value.filter((item) => item.room === '907')
    ytimes908S.value = ytimesS.value.filter((item) => item.room === '908')
  }
  return {
    ytimesS,
    ytimes905S,
    ytimes906S,
    ytimes907S,
    ytimes908S
  }
})
